package com.guigu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.ws.rs.DELETE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guigu.bean.Msg;
import com.guigu.bean.User;
import com.guigu.service.UserServices;

/**
 * @author 执子一白
 *处理用户CRUD请求
 */
@Controller
public class UserController {

	@Autowired
	UserServices userServices;
	
	/**
	 * 检查邮箱是否可用
	 * @param email
	 * @return
	 */
	@RequestMapping("/checkuser")
	@ResponseBody
	public Msg checkUser(@RequestParam(value="email", required=false)String email){
		if (email == null || email== "") {
			return Msg.fail().add("failInfo", "邮箱为空");
		}else {
			boolean b=userServices.checkUser(email);
			if (b) {
				return Msg.success();
			}else {
				return Msg.fail();
			}
		}
		
	}
	
	/**
	 * 用户修改方法
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/userCRUD/{userid}",method=RequestMethod.PUT)
	@ResponseBody
	public Msg updateUser(User user){
		System.out.println(user);
		userServices.updateUser(user);
		return Msg.success();
	}
	
	/**
	 * 用户删除方法
	 * @param userid
	 * @return
	 */
	@RequestMapping(value="/userCRUD/{userid}",method=RequestMethod.DELETE)
	@ResponseBody
	public Msg deleteUserByUserId(@PathVariable("userid")Integer userId){
		userServices.deleteUserByUserId(userId);
		return Msg.success();
	}
	
	/**
	 * 通过邮箱检索用户信息返回json数据
	 * @param email
	 * @return
	 * 默认值：
	 * @RequestParam(value="pn",defaultValue="1")
	 */
	@RequestMapping("/user")
	@ResponseBody
	public Msg getUserWithJson(@RequestParam(value="email", required=false)String email){
		if (email == null || email== "") {
			return Msg.fail().add("failInfo", "邮箱为空");
		}else {
			boolean b=userServices.checkUser(email);
			if (!b) {
				User user=userServices.getUserByEmail(email);
				return Msg.success().add("User", user);
			}else {
				return Msg.fail().add("User", "用户不存在");
			}
		}
		
		
	}
	
	/**
	 * 用户保存
	 * 1.支持JSR303校验
	 * 
	 * @param user
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/userCRUD",method=RequestMethod.POST)
	@ResponseBody
	public Msg saveUser(@Valid User user,BindingResult result){
		
		if (user.getEmail()==null) {
			return Msg.fail().add("registerStatus", "邮箱不可为空");
		}
		if (user.getPassword()==null) {
			return Msg.fail().add("registerStatus", "密码不可为空");
		}
		
		boolean b=userServices.checkUser(user.getEmail());
		if (b) {
			if (result.hasErrors()) {
				Map<String, Object> map =new HashMap<String, Object>();
				List<FieldError> errors =result.getFieldErrors();
				for (FieldError fieldError : errors) {
					System.out.println("错误字段名"+fieldError.getField());
					System.out.println("错误信息"+fieldError.getDefaultMessage());
					map.put(fieldError.getField(), fieldError.getDefaultMessage());
				}
				System.out.println("进入函数");
				return Msg.fail().add("errorFields", map).add("registerStatus", "注册失败");
			}else {
				userServices.saveUser(user);
				return Msg.success().add("registerStatus", "注册成功");
			}
		}else {
			return Msg.fail().add("registerStatus", "用户已存在");
		}
		
		
	}
	
	/**
	 * 用户登录方法检测用户是否存在，密码是否正确
	 * @param email
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Msg userLogin(@RequestParam("email")String email,@RequestParam("password")String password){
		
		boolean b=userServices.checkUser(email);
		
		if (!b) {
			User user= userServices.getUserByEmail(email);
			if (user.getPassword().equals(password)) {
				return Msg.success().add("loginStatus","密码正确");
			}else {
				return Msg.fail().add("loginStatus","密码错误");
			}
		}else{
			return Msg.fail().add("loginStatus","用户不存在!");
		}

	}
}
